package com.changcheng.controller;
import com.alibaba.fastjson.JSON;
import com.changcheng.pojo.Do.PageInfo;
import com.changcheng.pojo.Product;
import com.changcheng.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@Api(tags = "产品")
@RestController
public class ProductController {
    @Autowired
    ProductService productService;
    @ApiOperation("查询所有产品")
    @RequestMapping("/product/select")
    public String select(@RequestBody PageInfo pageInfo){
        HashMap<String,Object> res=new HashMap<>();
        //此处传递的是limit a,b中的a值
        pageInfo.setRemoveItem((pageInfo.getPage()-1)* pageInfo.getSumItem());
        List<Product> products = productService.select(pageInfo);
        //如果查询结果不为空
        if(products.size()>0){
            res.put("flag",0);
            res.put("data",products);
        }
        //如果查询结果为空
        else{
            res.put("flag",1);
        }
        return JSON.toJSONString(res);
    }

    @ApiOperation("查询产品的总个数(用于分页)")
    @RequestMapping("/product/selectItemsNumber")
    public int selectItemsNumber(){
        return productService.selectItemsNumber();
    }

    @ApiOperation("根据ID删除单个产品")
    @RequestMapping("/product/delete")
    public String delete(@RequestParam("id") int id){
        HashMap<String,Object> res=new HashMap<>();
        boolean delete = productService.delete(id);
        if(delete){
            res.put("flag",0);
        }else{
            res.put("flag",1);
        }
        return JSON.toJSONString(res);
    }

    @ApiOperation("上传图片")
    @RequestMapping("/product/upload")
    public String upload(HttpServletRequest request){
        HashMap<String,Object> res=new HashMap<>();
        String url=productService.upload(request);
        if(url!=null){
            res.put("flag",0);
            res.put("url",url);
        }else{
            res.put("flag",1);
        }
        return JSON.toJSONString(res);
    }

    @ApiOperation("修改产品")
    @RequestMapping("/product/update")
    public String update(@RequestBody Product product,HttpServletRequest request){
        HashMap<String,Object> res=new HashMap<>();
        boolean update = productService.update(product,request);
        if(update){
            res.put("flag",0);
        }else{
            res.put("flag",1);
        }
        return JSON.toJSONString(res);
    }

    @ApiOperation("根据ID修改产品的启用状态")
    @RequestMapping("/product/updateStateUsingById")
    public String updateStateUsingById(@RequestParam("id") Integer id,@RequestParam("state") Integer state){
        HashMap<String,Object> res=new HashMap<>();
        boolean result=productService.updateStateUsingById(id,state);
        if(result){
            res.put("flag",0);
        }else{
            res.put("flag",1);
        }
        return JSON.toJSONString(res);
    }

    @ApiOperation("添加产品")
    @RequestMapping("/product/addProduct")
    public String addProduct(@RequestBody Product product,HttpServletRequest request){
        HashMap<String,Object> res=new HashMap<>();
        boolean result=productService.add(product,request);
        if(result){
            res.put("flag",0);
        }else{
            res.put("flag",1);
        }
        return JSON.toJSONString(res);
    }

    @ApiOperation("客户端：调用后台产品接口")
    @RequestMapping("/product/customer/search")
    public String customer_search(){
        HashMap<String,Object> res=new HashMap<>();
        List<Product> products=productService.customer_search();
        if(products.size()>0){
            res.put("flag",0);
            res.put("data",products);
        }
        else{
            res.put("flag",1);
        }
        return JSON.toJSONString(res);
    }
}
